package io.rehuo.modules.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.rehuo.modules.app.dao.UserVaDao;
import io.rehuo.modules.app.entity.ApplyEntity;
import io.rehuo.modules.app.entity.PayConfigEntity;
import io.rehuo.modules.app.entity.UserVaEntity;
import io.rehuo.modules.app.entity.vo.AccountEntity;
import io.rehuo.modules.app.service.UserVaService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author shiguang
 * @date 2022年09月23日 22:49
 */
@Service("userVaService")
public class UserVaServiceImpl extends ServiceImpl<UserVaDao, UserVaEntity> implements UserVaService {

    @Override
    public UserVaEntity saveUserVaHqpay(JSONObject data, ApplyEntity applyEntity, PayConfigEntity payConfigEntity) {
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(data.getString("cardholder_name"));
        entity.setAccountNo(data.getString("card_number"));
        entity.setBank(data.getString("card_id"));
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        entity.setExpiredTime(data.getDate("expired_time"));
        entity.setRequestId(data.getString("request_id"));
        entity.setMerchantId(data.getString("merchant_id"));
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    public UserVaEntity saveUserVa(String data, ApplyEntity applyEntity, PayConfigEntity payConfigEntity) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONObject vaInfo = jsonObject.getJSONObject("vaInfo");
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(vaInfo.getString("accountName"));
        entity.setAccountNo(vaInfo.getString("accountNo"));
        entity.setBank(vaInfo.getString("bank"));
        entity.setSign(vaInfo.getString("sign"));
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    public void saveUserVaHqpayNew(AccountEntity accountEntity, ApplyEntity applyEntity, PayConfigEntity payConfigEntity, String batchId) {
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(accountEntity.getVaName());
        entity.setAccountNo(accountEntity.getVaNumber());
        entity.setBank(null);
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        entity.setExpiredTime(null);
        entity.setRequestId(batchId);
        entity.setMerchantId(batchId);
        entity.setType("2");
        baseMapper.insert(entity);
    }
}
